package com.yxhl.zoume.core.officialcar.fragment;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.OvershootInterpolator;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.jakewharton.rxbinding.view.RxView;
import com.yxhl.zoume.R;
import com.yxhl.zoume.UIConstants;
import com.yxhl.zoume.common.info.ToolbarType;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.common.ui.custom.RevealLayout;
import com.yxhl.zoume.core.car.activity.CarLocationMapActivity;
import com.yxhl.zoume.core.car.adapter.CarLocationMapAdapter;
import com.yxhl.zoume.core.car.model.SearchCityEvent;
import com.yxhl.zoume.core.car.model.SelectCityEvent;
import com.yxhl.zoume.core.expresscar.activity.ExpressCarContainerActivity;
import com.yxhl.zoume.core.func.map.presenter.location.LocationMapPresenter;
import com.yxhl.zoume.core.func.map.ui.fragment.BaseMapFragment;
import com.yxhl.zoume.core.officialcar.activity.OfficialCarContainerActivity;
import com.yxhl.zoume.core.specialcar.activity.SpecialCarContainerActivity;
import com.yxhl.zoume.core.specialcar.model.CarLocationMapEntrance;
import com.yxhl.zoume.core.specialcar.view.SpecialCarStationMapView;
import com.yxhl.zoume.data.rxbus.RxBus;
import com.yxhl.zoume.di.component.car.DaggerSpecialCarComponent;
import com.yxhl.zoume.di.component.car.SpecialCarComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.di.module.car.SpecialCarModule;
import com.yxhl.zoume.utils.DiskCacheUtils;
import com.yxhl.zoume.utils.LOG;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.recyclerview.animators.SlideInUpAnimator;
import rx.functions.Action1;


/**
 * 项目名称：Ferrari
 * Created by menggod on 16/9/16 .
 * <p>
 * 选择地点地图
 */
public class OfficialCarLocationMapFragment extends BaseMapFragment
        implements SpecialCarStationMapView, AMap.OnCameraChangeListener, AdapterView.OnItemSelectedListener {

    private static final String TAG = "OfficialCarLocationMapFragment";

    private static final String YI_YANG_CITY = "益阳市";
    private static final String CHANG_SHA_CITY = "长沙市";

    //益阳赫山区政府
    private static final LatLng YI_YANG_LAT_LNG = new LatLng(28.57641, 112.37558);
    //长沙市政府
    private static final LatLng CHANG_SHA_LAT_LNG = new LatLng(28.228209, 112.938814);


    private static final LatLng[] DEFAULT_SHOW_LAT_LNG_ARRAY = new LatLng[]{YI_YANG_LAT_LNG, CHANG_SHA_LAT_LNG};

    private static final float MAP_ZOOM_LEVEL = 14;

    /**
     * 周边搜索当前页
     */
    private static final int CURRENT_PAGE = 0;
    /**
     * ViewFlipper 播放间隔
     */
    private static final int FLIPPER_INTERVAL = 400;


    @BindView(R.id.rl_fragment_location_map_search)
    RelativeLayout mSearchRl;

//    @BindView(R.id.spinner_fragment_location_city_search)
//    Spinner mCitySpinner;

    @BindView(R.id.tv_select_city)
    TextView mSelectCity;

    @BindView(R.id.tv_fragment_location_map_search)
    TextView mLocationMapSearch;

    @BindView(R.id.ll_fragment_location_search)
    RevealLayout mLocationSearchLl;

    @BindView(R.id.map_include)
    MapView mMapView;

    @BindView(R.id.frame_fragment_location_car)
    FrameLayout mLocationFrame;
    @BindView(R.id.iv_special_car_location_icon)
    ImageView mLocateIv;

    @BindView(R.id.ll_include_poi_container)
    LinearLayout mPOIContainerLl;
    @BindView(R.id.iv_include_special_car_bottom_header)
    ImageView mBottomHeaderIv;
    @BindView(R.id.iv_include_transition_loading_icon)
    ImageView mLoadingIv;

    @BindView(R.id.rl_include_transition_loading_root)
    RelativeLayout mLoadingRootRl;
    @BindView(R.id.flipper_include_transition_token)
    ViewFlipper mLoadingFlipper;

    @BindView(R.id.tv_include_car_location_bottom_title)
    TextView mBottomTitleTv;

    @BindView(R.id.recycler_include_special_car_station_map)
    RecyclerView mRecyclerView;

    //arguments
    @Inject
    LocationMapPresenter mLocationMapPresenter;

    /**
     * 进入地点地图的入口类型
     */
    private CarLocationMapEntrance mEntrance;

    private CarLocationMapAdapter mSpecialCarPOIAdapter;

    /**
     * 图标定位 POI结果集
     */
    private List<PoiItem> mPoiItemList = new ArrayList<>();

    private Animation mPoiSearchLoadingAnimation;
    /**
     * 搜索栏点击次数
     */
    private int mSearchClickCount;

    /**
     * Spinner数据集
     */
    private List<String> mCityList;
    private ArrayAdapter<String> mSpinnerAdapter;
    private LatLng mCameraChangeLatLng;
    private boolean isFirst = true;

    /**
     * 是否从搜索页面跳到此页面
     */
    private boolean mFromPOISearchFragment = false;
    private PoiItem mLocationPoiItem;
    private Marker mCurrentLocationMarker;

    public OfficialCarLocationMapFragment() {
    }

    public static OfficialCarLocationMapFragment newInstance() {
        OfficialCarLocationMapFragment fragment = new OfficialCarLocationMapFragment();
        return fragment;
    }

    public static OfficialCarLocationMapFragment newInstance(CarLocationMapEntrance entrance) {
        OfficialCarLocationMapFragment fragment = new OfficialCarLocationMapFragment();
        Bundle arguments = new Bundle();
        arguments.putSerializable(UIConstants.LocationMap.ARGUMENT_KEY_LOCATION_MAP_FRAGMENT_ENTRANCE, entrance);
        fragment.setArguments(arguments);
        return fragment;
    }

    /**
     * 接受选择城市的回传信息
     */
    private void onRxBusEvent() {
        getSubscription().add(toSubscription(SelectCityEvent.class,
                new Action1<SelectCityEvent>() {
                    @Override
                    public void call(final SelectCityEvent selectCityEvent) {
                        if (selectCityEvent != null) {
                            String name = selectCityEvent.getName();
                            String center = selectCityEvent.getCenter();
                            if (!TextUtils.isEmpty(name)) {
                                mSelectCity.setText(name);
                                setCitySelected(name);
                            }
                            if (!TextUtils.isEmpty(center)) {
                                String[] point = center.split(",");
                                LatLng latLng = new LatLng(Double.parseDouble(point[1]), Double.parseDouble(point[0]));
                                animateMoveTo(latLng, MAP_ZOOM_LEVEL);
                                searchPOINearBy(latLng, name);

                            }
                        }
                    }
                }

        ));
    }

    private void parseArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mEntrance = (CarLocationMapEntrance) arguments.getSerializable(UIConstants.LocationMap.ARGUMENT_KEY_LOCATION_MAP_FRAGMENT_ENTRANCE);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onRxBusEvent();
        parseArguments();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mLocationMapPresenter.attachView(this);
        setMapListener(mMapView.getMap());
        initSearchBar(mEntrance);
        initBottomView();
        initPOISearchLoading();
        setCurrentCity();
        ButterKnife.bind(this, view);

        return view;
    }

    private void initMyLocation() {
        AMap aMap = mMapView.getMap();
        Location myLocation = aMap.getMyLocation();
        if (null != myLocation) {
            double latitude = myLocation.getLatitude();
            double longitude = myLocation.getLongitude();
            LatLng latLng = new LatLng(latitude, longitude);
            aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
        }
    }

    private void setCurrentCity() {
        if (mBaseLocationCity == null) {
            mSelectCity.setText("定位中");
        } else {
            mSelectCity.setText(mBaseLocationCity);
        }
    }

    /**
     * 单程分时切换定位的问题
     *
     * @param entrance
     */
    public void setEntrance(CarLocationMapEntrance entrance) {

        switch (mEntrance) {
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF: {
                if (entrance == CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_IN
                        || entrance == CarLocationMapEntrance.OFFICIAL_CAR_RENT_HOURLY_GET_IN) {
                    onLocationIconClick();
                }
                mEntrance = entrance;
                break;
            }
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN: {
                if (entrance == CarLocationMapEntrance.OFFICIAL_CAR_RENT_HOURLY_GET_IN
                        || entrance == CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_OFF) {
                    onLocationIconClick();
                }
                mEntrance = entrance;
                break;
            }
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN: {
                if (entrance == CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_IN
                        || entrance == CarLocationMapEntrance.OFFICIAL_CAR_SINGLE_WAY_GET_OFF) {
                    onLocationIconClick();
                }
                mEntrance = entrance;
                break;
            }
            default: {
                mEntrance = entrance;
                break;
            }
        }
    }

    public void setFromPOISearchFragment(boolean fromPOISearchFragment) {
        this.mFromPOISearchFragment = fromPOISearchFragment;
    }

    /**
     * UI状态重置
     */
    private void setInitialStatus() {
        //toolbar标题
        setupToolbarTitle(mEntrance);

        //搜索栏
        initSearchBar(mEntrance);

        //底栏图标
        setupBottomView(mEntrance);

        //定位图标
        if (mCurrentLocationMarker != null) {
            Bitmap iconBmp = getLocationMarkerBitmap();
            mCurrentLocationMarker.setIcon(BitmapDescriptorFactory.fromBitmap(iconBmp));
        }

        //GPS定位检查
        checkLocationEnable();
    }

    /**
     * 获取显示的定位图标
     *
     * @return
     */
    private Bitmap getLocationMarkerBitmap() {
        Bitmap iconBmp;
        switch (mEntrance) {
            case SPECIAL_CAR_START:
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
            case EXPRESS_CAR_RENT_HOURLY_GET_IN:
            case EXPRESS_CAR_SINGLE_WAY_GET_IN:
            case BUS_START_LOCATION:
                iconBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_shangche_location);
                break;
            case SPECIAL_CAR_ARRIVAL:
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
                iconBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_xiache_location);
                break;
            default:
                iconBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_shangche_location);
        }
        return iconBmp;
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        LOG.e(TAG, "onHiddenChanged hidden=" + hidden);
        if (!hidden) {
            setInitialStatus();
        }
    }

    private void setMapListener(AMap aMap) {
        //Map移动／拖动的监听
        mBaseStartMoveToLocation = true;
        aMap.setOnCameraChangeListener(this);
    }

    /**
     * 初始化搜索栏信息
     *
     * @param entrance
     */
    private void initSearchBar(CarLocationMapEntrance entrance) {
        switch (entrance) {
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
            case EXPRESS_CAR_RENT_HOURLY_GET_IN:
            case EXPRESS_CAR_SINGLE_WAY_GET_IN:
            case BUS_START_LOCATION:
                mLocationMapSearch.setHint(R.string.special_car_depart_search_hint);
                break;
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
                mLocationMapSearch.setHint(R.string.special_car_arrival_map_search_hint);
                break;
        }

    }

    /**
     * @param basicLocationCity 地图定位判断得出的城市
     * @return
     */
    private String getExtraLocationCity(String basicLocationCity) {
        if (basicLocationCity != null) {
            if (!YI_YANG_CITY.equals(basicLocationCity) && !CHANG_SHA_CITY.equals(basicLocationCity)) {
                return basicLocationCity;
            }
        } else {
            String locationCacheCity = DiskCacheUtils.getInstance(getContext()).getLocationCity();
            if (!YI_YANG_CITY.equals(locationCacheCity) && !CHANG_SHA_CITY.equals(locationCacheCity)) {
                return locationCacheCity;
            }
        }
        return "";
    }

    /**
     * 初始化加载动画
     */
    private void initPOISearchLoading() {
        mLoadingFlipper.setFlipInterval(FLIPPER_INTERVAL);
        mLoadingFlipper.startFlipping();
        mPoiSearchLoadingAnimation = AnimationUtils.loadAnimation(mActivity, R.anim.poi_search_loading);
        mLoadingIv.startAnimation(mPoiSearchLoadingAnimation);
    }

    /**
     * 初始化底栏
     */
    private void initBottomView() {
        setupBottomView(mEntrance);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mSpecialCarPOIAdapter = new CarLocationMapAdapter(getContext(), mPoiItemList, layoutManager);

        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setItemAnimator(new SlideInUpAnimator(new OvershootInterpolator(1f)));
        mRecyclerView.setAdapter(mSpecialCarPOIAdapter);
    }

    /**
     * 重置底栏信息
     */
    private void setupBottomView(CarLocationMapEntrance entrance) {
        switch (entrance) {
            case SPECIAL_CAR_START:
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
            case EXPRESS_CAR_RENT_HOURLY_GET_IN:
            case EXPRESS_CAR_SINGLE_WAY_GET_IN:
            case BUS_START_LOCATION:
                mBottomHeaderIv.setImageResource(R.drawable.ic_shangche);
                mLoadingIv.setImageResource(R.drawable.ic_shangche);
                mBottomTitleTv.setText(R.string.car_popup_get_in_header);
                break;
            case SPECIAL_CAR_ARRIVAL:
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
                mBottomHeaderIv.setImageResource(R.drawable.ic_xiache);
                mLoadingIv.setImageResource(R.drawable.ic_xiache);
                mBottomTitleTv.setText(R.string.car_popup_get_off_header);
                break;
        }
    }

    /**
     * 设置toolbar标题内容
     *
     * @param entrance
     */
    private void setupToolbarTitle(CarLocationMapEntrance entrance) {
        if (entrance != null) {
            String toolbarTitle;
            switch (entrance) {
                case SPECIAL_CAR_START:
                case BUS_START_LOCATION:
                    toolbarTitle = getString(R.string.special_car_station_depart_header_title);
                    break;
                case SPECIAL_CAR_ARRIVAL:
                    toolbarTitle = getString(R.string.special_car_station_arrival_header_title);
                    break;
                case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
                case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                case EXPRESS_CAR_SINGLE_WAY_GET_IN:
                case EXPRESS_CAR_RENT_HOURLY_GET_IN:
                    toolbarTitle = getString(R.string.official_car_get_in_location_header_title);
                    break;
                case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
                case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
                    toolbarTitle = getString(R.string.official_car_get_off_location_header_title);
                    break;
                default:
                    toolbarTitle = "";
            }
            switch (mEntrance) {
                case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
                case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
                case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                    if (mActivity instanceof OfficialCarContainerActivity) {
                        ((OfficialCarContainerActivity) mActivity).changeToolbarInto(ToolbarType.DEFAULT);
                        ((OfficialCarContainerActivity) mActivity).setToolbarTitle(toolbarTitle);
                    }
                    break;
                case EXPRESS_CAR_SINGLE_WAY_GET_IN:
                case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
                case EXPRESS_CAR_RENT_HOURLY_GET_IN:
                case BUS_START_LOCATION:
                    if (mActivity instanceof CarLocationMapActivity)
                        ((BaseActivityWithToolBar) mActivity).setToolbarTitle(toolbarTitle);
                    break;

            }

        }
    }

    @Override
    protected void onMapLocationChange() {
        super.onMapLocationChange();

        if (isFirst) {
            mSelectCity.setText(mBaseLocationCity);
            RxBus.getInstance().send(new SearchCityEvent(mSelectCity.getText().toString().trim()));
            isFirst = false;
        }
    }

    @OnClick(R.id.ll_fragment_location_search)
    public void onSearchClick(View view) {
        mSearchClickCount++;
        RxView.clicks(view).throttleFirst(500, TimeUnit.MICROSECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                LOG.e(TAG, "onSearchClick call");
                mSearchClickCount = 0;
                searchClickHandler(mEntrance);
            }
        });
        if (mSearchClickCount == 1) {
            mSearchRl.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (mSearchClickCount == 1) {
                        LOG.e(TAG, "onSearchClick postDelayed");
                        searchClickHandler(mEntrance);
                    }
                }
            }, 200);
        }
    }

    @OnClick(R.id.tv_select_city)
    public void onSelcetCityClick() {
        switch (mEntrance) {
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                ((OfficialCarContainerActivity) getActivity()).displayOneFragment(OfficialCarContainerActivity.FRAGMENT_SELECT_CITY, mEntrance);
                break;
            case EXPRESS_CAR_SINGLE_WAY_GET_IN:
            case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
            case EXPRESS_CAR_RENT_HOURLY_GET_IN:
                ((CarLocationMapActivity) mActivity).switchSelectCityFragment();
                break;
        }
    }

    /**
     * 响应查询点击
     */
    private void searchClickHandler(CarLocationMapEntrance entrance) {
        switch (mEntrance) {
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                ((OfficialCarContainerActivity) mActivity).displayOneFragment(OfficialCarContainerActivity.FRAGMENT_CAR_POI_SEARCH, entrance);
                break;
            case EXPRESS_CAR_SINGLE_WAY_GET_IN:
            case EXPRESS_CAR_SINGLE_WAY_GET_OFF:
            case EXPRESS_CAR_RENT_HOURLY_GET_IN:
                ((CarLocationMapActivity) mActivity).switchPOISearchFragment();
                break;
        }
        RxBus.getInstance().send(new SearchCityEvent(mSelectCity.getText().toString().trim()));
    }


    @OnClick(R.id.frame_fragment_location_car)
    public void onLocationIconClick() {
        if (mBaseLocationCity != null) {
            String currentSelectedCity;
            switch (mEntrance) {
                case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
                case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
                case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                    currentSelectedCity = ((OfficialCarContainerActivity) mActivity).getUserSelectCity();
                    break;
                default:
                    currentSelectedCity = "";
            }
            if (mBaseLocationCity.equals(currentSelectedCity)) {
                Location location = mMapView.getMap().getMyLocation();
                if (location != null) {
                    animateMoveTo(new LatLng(location.getLatitude(), location.getLongitude()), MAP_ZOOM_LEVEL);
                }
            } else {
                Location location = mMapView.getMap().getMyLocation();
                if (location != null) {
                    animateMoveTo(new LatLng(location.getLatitude(), location.getLongitude()), MAP_ZOOM_LEVEL);
                    mSelectCity.setText(mBaseLocationCity);
                }
            }
        }
    }

    /**
     * 设置选中城市
     *
     * @param name
     */
    private void setCitySelected(String name) {
        switch (mEntrance) {
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                ((OfficialCarContainerActivity) mActivity).setUserSelectCity(name);
                break;
        }
    }

    /**
     * 调用spinner.setSelection()也会调用此方法
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String currentSelectedCity = mCityList.get(position);
        switch (mEntrance) {
            case SPECIAL_CAR_START:
            case SPECIAL_CAR_ARRIVAL:
                ((SpecialCarContainerActivity) mActivity).setUserSelectCity(currentSelectedCity);
                break;
            case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
            case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
            case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                ((OfficialCarContainerActivity) mActivity).setUserSelectCity(currentSelectedCity);
                break;
            case EXPRESS_CAR_SINGLE_WAY_GET_IN:
            case EXPRESS_CAR_RENT_HOURLY_GET_IN:
                ((ExpressCarContainerActivity) mActivity).setUserSelectCity(currentSelectedCity);
                break;
        }

        if (currentSelectedCity.equals(mBaseLocationCity)) {
            Location location = mMapView.getMap().getMyLocation();
            if (location != null) {
                animateMoveTo(new LatLng(location.getLatitude(), location.getLongitude()), MAP_ZOOM_LEVEL);
            } else {
                if (position <= DEFAULT_SHOW_LAT_LNG_ARRAY.length - 1) {
                    animateMoveTo(DEFAULT_SHOW_LAT_LNG_ARRAY[position], MAP_ZOOM_LEVEL);
                }
            }
        } else {
            if (position <= DEFAULT_SHOW_LAT_LNG_ARRAY.length - 1) {
                animateMoveTo(DEFAULT_SHOW_LAT_LNG_ARRAY[position], MAP_ZOOM_LEVEL);
            }
        }

        searchPOINearBy(mCameraChangeLatLng, currentSelectedCity);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }

    /**
     * 地图拖动监听
     *
     * @param cameraPosition
     */
    @Override
    public void onCameraChange(CameraPosition cameraPosition) {
    }

    @Override
    public void onCameraChangeFinish(CameraPosition cameraPosition) {
        if (cameraPosition != null) {
            mCameraChangeLatLng = cameraPosition.target;
            String currentSelectedCity;
            switch (mEntrance) {
                case SPECIAL_CAR_START:
                case SPECIAL_CAR_ARRIVAL:
                    currentSelectedCity = ((SpecialCarContainerActivity) mActivity).getUserSelectCity();
                    break;
                case OFFICIAL_CAR_SINGLE_WAY_GET_IN:
                case OFFICIAL_CAR_SINGLE_WAY_GET_OFF:
                case OFFICIAL_CAR_RENT_HOURLY_GET_IN:
                    currentSelectedCity = ((OfficialCarContainerActivity) mActivity).getUserSelectCity();
                    break;
                case EXPRESS_CAR_SINGLE_WAY_GET_IN:
                case EXPRESS_CAR_RENT_HOURLY_GET_IN:
//                    currentSelectedCity = ((ExpressCarContainerActivity) mActivity).getUserSelectCity();
                    break;
                default:
                    currentSelectedCity = "";
            }
            searchPOINearBy(mCameraChangeLatLng, mSelectCity.getText().toString().trim());

            //    mCitySpinner.setEnabled(true);
        }
    }

    /**
     * 查询附近的POI点
     *
     * @param latLng
     */
    private void searchPOINearBy(LatLng latLng, String selectedCity) {
        if (latLng != null) {
            LatLonPoint centerPoint = new LatLonPoint(latLng.latitude, latLng.longitude);
            mLocationMapPresenter.doSearchPOINearBy(mActivity, CURRENT_PAGE, selectedCity, centerPoint);
        }
    }

    public String getCityText() {
        return mSelectCity.getText().toString().trim();
    }

    @Override
    public void renderPOISearchResult(List<PoiItem> poiItems) {
        if (poiItems != null && poiItems.size() > 0) {
            mLocationPoiItem = poiItems.get(0);
        }
        closeLoadingAnimation();
        if (mLoadingRootRl != null) {
            mLoadingRootRl.setVisibility(View.GONE);
        }
        mSpecialCarPOIAdapter.notifyItemData(poiItems);
    }

    /**
     * 关闭加载动画
     */
    private void closeLoadingAnimation() {
        if (mPoiSearchLoadingAnimation != null) {
            mPoiSearchLoadingAnimation.cancel();
        }
        if (mLoadingIv != null) {
            mLoadingIv.clearAnimation();
        }

        if (mLoadingFlipper != null) {
            if (mLoadingFlipper.isFlipping()) {
                mLoadingFlipper.stopFlipping();
            }
        }
    }

    @Override
    public void renderPOISearchOnFailure() {
    }


    public PoiItem getLocationPoiItem() {
        return mLocationPoiItem;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LOG.e(TAG, "onDestroyView");
        //1.停止网络,停止动画
        if (mPoiSearchLoadingAnimation != null) {
            mPoiSearchLoadingAnimation.cancel();
            mPoiSearchLoadingAnimation = null;
        }
    }

    @Override
    protected boolean setUpLocationStyle(AMap aMap) {
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.icon_user_location));
        myLocationStyle.radiusFillColor(Color.TRANSPARENT);
        myLocationStyle.strokeColor(Color.TRANSPARENT);
        aMap.setMyLocationStyle(myLocationStyle);
        return true;
    }

    @Override
    protected Marker getLocationMarker(AMap aMap, LatLng latLng) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions
                .draggable(true)
                .setFlat(true)
                .icon(BitmapDescriptorFactory.fromBitmap(getLocationMarkerBitmap()))
                .position(latLng)
                .anchor(0.5f, 1);

        // 计算定位图标在屏幕显示的位置
        int centerX = mMapView.getWidth() / 2;
        int centerY = mMapView.getHeight() / 2;
        //屏幕拖动,图标不动
        mCurrentLocationMarker = aMap.addMarker(markerOptions);
        mCurrentLocationMarker.setPositionByPixels(centerX, centerY);

        return mCurrentLocationMarker;
    }

    @Override
    protected float getLocationMapZoomLevel() {
        return MAP_ZOOM_LEVEL;
    }

    @Override
    protected void secondAnimateMove() {

    }

    @Override
    protected void initializeInjector() {
        SpecialCarComponent specialCarComponent = DaggerSpecialCarComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(mActivity))
                .specialCarModule(new SpecialCarModule())
                .build();
        specialCarComponent.inject(this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_car_location_map;
    }

    @Override
    protected void setupToolBar() {
        setupToolbarTitle(mEntrance);
    }

}
